package cn.itcast.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.dao.PermissionDao;
import cn.itcast.dao.RoleDao;
import cn.itcast.dao.UserDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;

import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Set;

@Slf4j
@Service(interfaceClass = UserService.class)
@Transactional //事务注解
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    //分页查询用户
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据分页查询信息
        Page<User> users = this.userDao.selectByCondition(queryPageBean.getQueryString());
        //封装结果 并返回
        PageResult pageResult = new PageResult(users.getTotal(), users.getResult());

        return pageResult;
    }


    //添加用户
    @Transactional
    @Override
    public void add(Integer[] roleIds, User user) {
        //先向User表中插入user信息,并返回userId
        boolean b1 = this.userDao.add(user);

        //将userId 和 roleIds插入中间表
        for (Integer roleId : roleIds) {
            this.userDao.insertUserAndRoleId(user.getId(), roleId);
        }

    }


    //根据id查询User
    @Override
    public User findUserById(Integer userId) {
        User user = this.userDao.findUserById(userId);
        return user;
    }

    //根据id查询Roles
    @Override
    public ArrayList<Integer> findRolesById(Integer userId) {
        ArrayList<Integer> roleIds = this.userDao.findRolesById(userId);
        return roleIds;
    }

    //修改用户信息
    @Override
    @Transactional
    public void editUser(Integer[] NewRoleIds, User user) {

        //1.先根据userId在User表中修改user
        this.userDao.updateUser(user);
        log.debug("插入user后返回的userId", user.getId());

        //2.根据id修改中间表
        //2.1删除所有的userId对应的中间表数据
        this.userDao.deleteUserAndRoleId(user.getId());

        //2.2重新给中间表插入数据
        for (Integer roleId : NewRoleIds) {
            this.userDao.insertUserAndRoleId(user.getId(), roleId);
        }
    }


    //删除用户
    @Override
    @Transactional
    public void deleteUser(Integer userId) {

        //删除中间表
        this.userDao.deleteUserAndRoleId(userId);
        //删除用户
        this.userDao.deleteUser(userId);
    }

    //根据用户名，查询用户信息，角色信息，权限信息
    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息
        if (ObjectUtil.isEmpty(user)) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户id查询角色
        Set<Role> roles = roleDao.findByUserId(userId);
        //根据角色查询权限
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色查询权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }
}
